package com.qf.test.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.test.pojo.DtsRegion;
import com.qf.test.service.RegionService;
import com.qf.test.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//请求网址: http://127.0.0.1:8083/admin/region/list?page=1&limit=20
//把这个类交给spring管理
@RestController
//解决前后端的分离问题
@CrossOrigin("*")
@RequestMapping("/region")
public class Regioncontroller {
    @Autowired
    private RegionService regionService;

    /**
     * @param page  当前页  默认为1
     * @param limit 每页多少条数据默认为10
     * @return
     */
    @GetMapping("/list")
    public Object list(String name, String code,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer limit) {
        //1.返回分分数据
        IPage<DtsRegion> iPage = regionService.queryDtsRegion(name, code, page, limit);
        //2.获取结果集
        List<DtsRegion> list = iPage.getRecords();
        //3.获取总条数
        long count = iPage.getTotal();
        //4.封装对象返回
        Map<String, Object> data = new HashMap<>();
        data.put("total",count);
        data.put("items",list );
        return ResponseUtil.ok(data);
    }
}
